<?php
namespace Admin\Controller;
    use Think\Controller;
	use Think\Category;
    class HooksController extends CommonController
    {
        public function index()
        {
            $Blog = M('hooks');
            $count = $Blog->count();
            $Page = new \Think\Page($count, 20);
            $show = $Page->show();
            $list = $Blog->order('id ASC')->limit(($Page->firstRow . ',') . $Page->listRows)->select();
            $this->assign('list', $list);
            $this->assign('page', $show);
            $this->display('hooks_list');
        }
        public function Article_add()
        {

            $cate=M('category')->select();
			$cate = Category::zifenlei($cate,'&nbsp;&nbsp;|--');
            $this->assign('cate',$cate);
            $this->display('write');
        }
        public function addcontent()
        {
            $upload = new \Think\Upload();
            $upload->maxSize = C('mi_maxSize');
		   $mi_extss =explode(',',C('mi_exts'));
           $upload->exts=$mi_extss;

            $upload->savePath = './Uploads/thumbnail/';
            $upload->autoSub = true;
            $upload->subName = array('date', 'Ymd');


            $info = $upload->upload();
            foreach ($info as $file) {
              $img=  $file['savepath'] . $file['savename'];
            }
            $article = M('article');
            $data['title'] = $_POST['title'];
            if(empty(I('post.content'))){
                $this->error("内容不能为空");
            }
            $data['content'] = I('post.content');
            $data['cid'] = I('post.cid');

            if ($_POST['time'] == '') {
                $data['time'] = time();
            } else {
                $data['time'] = strtotime($_POST['time']);
            }
            $data['tag'] = I('post.tag');
            $data['click'] = I('post.click');
            $data['hot'] = I('post.hot');
            $data['summary'] = I('post.summary');

            if ($img == '') {
                $data['img'] = null;
            } else {
                $data['img'] = $img;
            }



            if ($article->add($data)) {
                $this->success('添加成功');
            } else {
                $this->error('添加失败');
            }
        }


        public function mod()
        {

            $id = I('get.id');

            if (!empty($id)) {
                $art = M('Article');
                $date = $art->where(array('aid' => $id))->find($id);
                $this->assign('conn', $date);
                $cat = M('category')->select();
				$cat = Category::zifenlei($cat,'&nbsp;&nbsp;|--');
                $this->assign('clist', $cat);
            }

            $this->display('article_mod');
        }
        public function del()
        {
            if (IS_POST) {
                $ids = $_POST;
                $db = M('hooks');
                if ($ids) {
                    foreach ($ids as $id) {
                        $db->where(array('id' => $id))->delete();
                    }
                }
                $this->success('<p>'.'删除成功'.'</p>');
            } else {
                $id = I('get.id');
                $db = M('Article');
                $status = $db->where(array('aid' => $id))->delete();
                if ($status) {
                    $this->success('删除成功');
                } else {
                    $this->error('删除失败');
                }
            }
        }

        public function update()
        {

            $Form = M('hooks');

            if ($Form->create()) {

                $result = $Form->save();
                if ($result) {
                    $this->success('更新成功');
                } else {
                    $this->error('更新失败');
                }
            } else {
               // $this->error($Form->getError(L('error')));
            }
        }
    }
